package com.scs.application.modules.wm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.scs.application.core.mvc.controller.FlowController;
import com.scs.application.core.query.QueryContext;
import com.scs.application.core.query.QueryContextHolder;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.core.utils.UserUtils;
import com.scs.application.modules.base.entity.Dept;
import com.scs.application.modules.base.service.DeptService;
import com.scs.application.modules.base.service.UserWarehouseService;
import com.scs.application.modules.base.vo.UserWarehouseVO;
import com.scs.application.modules.report.handler.BeforeReport;
import com.scs.application.modules.sys.model.PageResult;
import com.scs.application.modules.wm.entity.StockDeptBillItem;
import com.scs.application.modules.wm.service.StockDeptBilltemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 三级库库存
 * </p>
 */
@RestController
@RequestMapping("/wm/stock_dept_bill_item")
public class StockDeptBillItemController extends FlowController<StockDeptBilltemService, StockDeptBillItem> {

    @Autowired
    private UserWarehouseService userWarehouseService;

    @Autowired
    private DeptService deptService;

    /**
     * 获取数据列表
     */
    @Override
    @GetMapping("/page")
    public PageResult page() {
        QueryContext queryContext = QueryContextHolder.currentContext();

        //非全院权限
        if(!BeforeReport.getFlagAllData()){
            //登录用户可用工位
            List<UserWarehouseVO> userWarehouseList = userWarehouseService.getUserWarehouse();
            List<String> ids = new ArrayList<>();
            userWarehouseList.forEach(userWarehouse -> {
                if (StringUtils.isNotBlank(userWarehouse.getDeptId())) {
                    ids.add(userWarehouse.getDeptId());
                }
            });
            ids.add(UserUtils.currentUser().getDeptId());
            List<Dept> depts=deptService.list(Wrappers.<Dept>query().in("id",ids));
            List<String> hisCodeList = depts.stream().map(Dept::getHisCode).collect(Collectors.toList());
            if (hisCodeList.isEmpty()) {
                hisCodeList.add("-1");
            }
            QueryWrapper<StockDeptBillItem> queryWrapper = queryContext.getWrapper();
            queryWrapper.and(wrapper ->
                    wrapper.in("dept_code_use", hisCodeList).or()
                            .in("dept_code_exec", hisCodeList)
            );
        }

        IPage pageResult = service.page(queryContext);
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}
